home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume17 / cproto / part02 < prev    next >
Encoding:
Internet Message Format  |  1991-03-25  |  47.8 KB

  1. From: cthuang@contact.UUCP (Chin Huang)
  2. Newsgroups: comp.sources.misc
  3. Subject: v17i071:  cproto - Generate C function prototypes from C source, Part02/02
  4. Message-ID: <1991Mar25.221201.22551@sparky.IMD.Sterling.COM>
  5. Date: 25 Mar 91 22:12:01 GMT
  6. Approved: kent@sparky.imd.sterling.com
  7. X-Checksum-Snefru: ce869bec 12106b73 4d93aea9 8fa9d9cb
  8.  
  9. Submitted-by: Chin Huang <cthuang@contact.UUCP>
  10. Posting-number: Volume 17, Issue 71
  11. Archive-name: cproto/part02
  12.  
  13. #! /bin/sh
  14. # This is a shell archive.  Remove anything before this line, then unpack
  15. # it by saving it into a file and typing "sh file".  To overwrite existing
  16. # files, type "sh file -c".  You can also feed this as standard input via
  17. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  18. # will see the following message at the end:
  19. #        "End of shell archive."
  20. # Contents:  lex.l grammar.y config.h cproto.h patchlev.h semantic.h
  21. #   symbol.h cproto.c semantic.c string.c symbol.c
  22. # Wrapped by ibmpc@laphroig.UUCP on Mon Mar 25 11:41:06 1991
  23. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  24. if test -f lex.l -a "${1}" != "-c" ; then 
  25.   echo shar: Will not over-write existing file \"lex.l\"
  26. else
  27. echo shar: Extracting \"lex.l\" \(4859 characters\)
  28. sed "s/^X//" >lex.l <<'END_OF_lex.l'
  29. X%{
  30. X/* $Id: lex.l 2.1 91/03/25 11:40:27 cthuang Exp $
  31. X *
  32. X * C function prototype generator
  33. X * Lexical analyzer specification
  34. X */
  35. X%}
  36. X
  37. XWS        [ \t]
  38. XWLF        [ \t\n\f]*
  39. XLETTER        [A-Za-z_]
  40. XDIGIT        [0-9]
  41. XID        {LETTER}({LETTER}|{DIGIT})*
  42. XQUOTED        (\"(\\\"|[^"\n])*\"|\'.\'|\\.)
  43. X
  44. X%{
  45. Xchar cur_file[MAX_TEXT_LENGTH];    /* current file name */
  46. Xint line_num = 1;        /* current line number in file */
  47. Xstatic int curly = 0;        /* number of curly brace nesting levels */
  48. X
  49. Xtypedef struct {
  50. X    FILE *fp;
  51. X    char *file;
  52. X    int line_num;
  53. X} IncludeStack;
  54. X
  55. Xstatic int inc_depth = 0;            /* include nesting level */
  56. Xstatic IncludeStack inc_stack[MAX_INC_DEPTH];    /* stack of included files */
  57. Xstatic void do_include();
  58. X%}
  59. X
  60. X%s CPP1 CPP2 INIT1 INIT2 CURLY COMMENT
  61. X%%
  62. X
  63. X\n            ++line_num;
  64. X
  65. X<INITIAL>^#{WS}*    BEGIN CPP1;
  66. X<CPP1>define{WS}+{ID}.*\\$    {
  67. X                sscanf(yytext, "define %s", buf);
  68. X                new_symbol(typedef_names, buf);
  69. X                BEGIN CPP2;
  70. X            }
  71. X<CPP1>define{WS}+{ID}.*$    {
  72. X                sscanf(yytext, "define %s", buf);
  73. X                new_symbol(typedef_names, buf);
  74. X                BEGIN INITIAL;
  75. X            }
  76. X<CPP2>.*\\$        ;
  77. X<CPP2>.*$        BEGIN INITIAL;
  78. X
  79. X<CPP1>include{WS}+\"[^"]+\".*$    {
  80. X                sscanf(yytext, "include \"%[^\"]\"", buf);
  81. X                do_include(buf, 0);
  82. X                BEGIN INITIAL;
  83. X            }
  84. X<CPP1>include{WS}+\<[^>]+\>.*$    {
  85. X                sscanf(yytext, "include <%[^>]>", buf);
  86. X                do_include(buf, 1);
  87. X                BEGIN INITIAL;
  88. X            }
  89. X
  90. X<CPP1>[0-9]+{WS}+\".*$    {
  91. X                sscanf(yytext, "%d \"%[^\"]\"", &line_num,
  92. X                 cur_file);
  93. X                --line_num;
  94. X                BEGIN INITIAL;
  95. X            }
  96. X<CPP1>[0-9]+.*$        {
  97. X                sscanf(yytext, "%d ", &line_num);
  98. X                --line_num;
  99. X                BEGIN INITIAL;
  100. X            }
  101. X
  102. X<CPP1>.*$        BEGIN INITIAL;
  103. X
  104. X<INITIAL>"("        return '(';
  105. X<INITIAL>")"        return ')';
  106. X<INITIAL>"*"        return '*';
  107. X<INITIAL>","        return ',';
  108. X<INITIAL>";"        return ';';
  109. X<INITIAL>"..."        return T_ELLIPSIS;
  110. X
  111. X<INITIAL>auto        return T_AUTO;
  112. X<INITIAL>extern        return T_EXTERN;
  113. X<INITIAL>register    return T_REGISTER;
  114. X<INITIAL>static        return T_STATIC;
  115. X<INITIAL>typedef    return T_TYPEDEF;
  116. X<INITIAL>char        return T_CHAR;
  117. X<INITIAL>double        return T_DOUBLE;
  118. X<INITIAL>float        return T_FLOAT;
  119. X<INITIAL>int        return T_INT;
  120. X<INITIAL>void        return T_VOID;
  121. X<INITIAL>long        return T_LONG;
  122. X<INITIAL>short        return T_SHORT;
  123. X<INITIAL>signed        return T_SIGNED;
  124. X<INITIAL>unsigned    return T_UNSIGNED;
  125. X<INITIAL>enum        return T_ENUM;
  126. X<INITIAL>struct        return T_STRUCT;
  127. X<INITIAL>union        return T_UNION;
  128. X<INITIAL>const        return T_CONST;
  129. X<INITIAL>volatile    return T_VOLATILE;
  130. X<INITIAL>inline        return T_INLINE;
  131. X<INITIAL>\"C\"        return T_QUOTEC;
  132. X<INITIAL>cdecl        return T_CDECL;
  133. X<INITIAL>far        return T_FAR;
  134. X<INITIAL>huge        return T_HUGE;
  135. X<INITIAL>interrupt    return T_INTERRUPT;
  136. X<INITIAL>near        return T_NEAR;
  137. X<INITIAL>pascal        return T_PASCAL;
  138. X
  139. X<INITIAL>\[[^\]]*\]    {
  140. X                strcpy(yylval.text, yytext);
  141. X                return T_BRACKETS;
  142. X            }
  143. X
  144. X<INITIAL>{ID}        {
  145. X                strcpy(yylval.text, yytext);
  146. X                if (is_typedef_name(yytext))
  147. X                return T_TYPEDEF_NAME;
  148. X                else
  149. X                return T_IDENTIFIER;
  150. X            }
  151. X
  152. X<INITIAL>"="{WLF}"{"    {
  153. X                int i;
  154. X
  155. X                curly = 1;
  156. X                BEGIN INIT1;
  157. X                for (i = 0; i < yyleng; ++i)
  158. X                if (yytext[i] == '\n') ++line_num;
  159. X            }
  160. X<INIT1>"{"        ++curly;
  161. X<INIT1>"}"        {
  162. X                if (--curly == 0) {
  163. X                BEGIN INITIAL;
  164. X                return T_INITIALIZER;
  165. X                }
  166. X            }
  167. X<INIT1>{QUOTED}|.    ;
  168. X
  169. X<INITIAL>"="        BEGIN INIT2;
  170. X<INIT2>[,;]        {
  171. X                unput(yytext[yyleng-1]);
  172. X                BEGIN INITIAL;
  173. X                return T_INITIALIZER;
  174. X            }
  175. X<INIT2>{QUOTED}|.    ;
  176. X
  177. X<INITIAL>"{"        { curly = 1; BEGIN CURLY; }
  178. X<CURLY>"{"        ++curly;
  179. X<CURLY>"}"        {
  180. X                if (--curly == 0) {
  181. X                BEGIN INITIAL;
  182. X                return T_BRACES;
  183. X                }
  184. X            }
  185. X<CURLY>{QUOTED}|.    ;
  186. X
  187. X<INITIAL>"/*"        BEGIN COMMENT;
  188. X<COMMENT>"*/"        BEGIN INITIAL;
  189. X<COMMENT>.        ;
  190. X
  191. X<INITIAL>"//".*$    ;
  192. X
  193. X[ \t\f]+        ;
  194. X.            {
  195. X                output_error();
  196. X                fprintf(stderr, "bad character '%c'\n", yytext[0]);
  197. X            }
  198. X%%
  199. X
  200. X/* Process include directive.
  201. X */
  202. Xstatic void
  203. Xdo_include (filename, sysinc)
  204. Xchar *filename;        /* file name */
  205. Xint sysinc;        /* 1 = do not search current directory */
  206. X{
  207. X    char path[MAX_TEXT_LENGTH];
  208. X    int i;
  209. X    FILE *fp;
  210. X    IncludeStack *sp;
  211. X
  212. X    if (inc_depth >= MAX_INC_DEPTH) {
  213. X    output_error();
  214. X    fprintf(stderr, "includes too deeply nested\n");
  215. X    return;
  216. X    }
  217. X
  218. X    for (i = sysinc != 0; i < num_inc_dir; ++i) {
  219. X     strcpy(path, inc_dir[i]);
  220. X     strcat(path, filename);
  221. X     if ((fp = fopen(path, "r")) != NULL) {
  222. X         sp = inc_stack + inc_depth;
  223. X         sp->fp = yyin;
  224. X         sp->file = strdup(cur_file);
  225. X         sp->line_num = line_num;
  226. X         ++inc_depth;
  227. X         yyin = fp;
  228. X         strcpy(cur_file, filename);
  229. X         line_num = 0;
  230. X         return;
  231. X     }
  232. X    }
  233. X}
  234. X
  235. X/* When the end of the current input file is reached, pop any
  236. X * nested includes.
  237. X */
  238. Xstatic int
  239. Xyywrap ()
  240. X{
  241. X    IncludeStack *sp;
  242. X
  243. X    if (inc_depth > 0) {
  244. X    --inc_depth;
  245. X    sp = inc_stack + inc_depth;
  246. X    fclose(yyin);
  247. X    yyin = sp->fp;
  248. X    strcpy(cur_file, sp->file);
  249. X    free(sp->file);
  250. X    line_num = sp->line_num + 1;
  251. X    return 0;
  252. X    } else {
  253. X    return 1;
  254. X    }
  255. X}
  256. END_OF_lex.l
  257. if test 4859 -ne `wc -c <lex.l`; then
  258.     echo shar: \"lex.l\" unpacked with wrong size!
  259. fi
  260. # end of overwriting check
  261. fi
  262. if test -f grammar.y -a "${1}" != "-c" ; then 
  263.   echo shar: Will not over-write existing file \"grammar.y\"
  264. else
  265. echo shar: Extracting \"grammar.y\" \(10073 characters\)
  266. sed "s/^X//" >grammar.y <<'END_OF_grammar.y'
  267. X/* $Id: grammar.y 2.1 91/02/28 11:16:07 cthuang Exp $
  268. X *
  269. X * yacc grammar for C prototype generator
  270. X * This was derived from the grammar given in Appendix A of
  271. X * "The C Programming Language" by Kernighan and Ritchie.
  272. X */
  273. X
  274. X/* identifiers that are not reserved words */
  275. X%token T_IDENTIFIER T_TYPEDEF_NAME
  276. X
  277. X/* storage class */
  278. X%token T_AUTO T_EXTERN T_REGISTER T_STATIC T_TYPEDEF
  279. X/* This keyword included for compatibility with C++. */
  280. X%token T_INLINE
  281. X
  282. X/* type specifiers */
  283. X%token T_CHAR T_DOUBLE T_FLOAT T_INT T_VOID
  284. X%token T_LONG T_SHORT T_SIGNED T_UNSIGNED
  285. X%token T_ENUM T_STRUCT T_UNION
  286. X
  287. X/* type qualifiers */
  288. X%token T_CONST T_VOLATILE
  289. X/* These keywords included for compatibility with MSDOS C compilers. */
  290. X%token T_CDECL T_FAR T_HUGE T_INTERRUPT T_NEAR T_PASCAL
  291. X
  292. X/* paired braces and everything between them: { ... } */
  293. X%token T_BRACES
  294. X
  295. X/* paired square brackets and everything between them: [ ... ] */
  296. X%token T_BRACKETS
  297. X
  298. X/* three periods */
  299. X%token T_ELLIPSIS
  300. X
  301. X/* equal sign followed by constant expression or stuff between braces */
  302. X%token T_INITIALIZER
  303. X
  304. X/* "C" */
  305. X%token T_QUOTEC
  306. X
  307. X%type <decl_spec> declaration_specifiers declaration_specifier
  308. X%type <decl_spec> storage_class type_specifier type_qualifier
  309. X%type <decl_spec> struct_or_union_specifier enum_specifier
  310. X%type <decl_list> init_declarator_list
  311. X%type <declarator> init_declarator declarator direct_declarator
  312. X%type <declarator> abs_declarator direct_abs_declarator
  313. X%type <param_list> parameter_type_list parameter_list
  314. X%type <parameter> parameter_declaration
  315. X%type <param_list> opt_identifier_list identifier_list
  316. X%type <text> struct_or_union
  317. X%type <text> pointer type_qualifiers
  318. X%type <text> any_id T_IDENTIFIER T_TYPEDEF_NAME
  319. X%type <text> T_BRACKETS
  320. X
  321. X%{
  322. X#include <stdio.h>
  323. X#include "cproto.h"
  324. X#include "semantic.h"
  325. X
  326. X#define YYMAXDEPTH 150
  327. X
  328. X/* This variable is set TRUE when we are scanning the parameter declarations
  329. X * part of a function definition.
  330. X */
  331. Xstatic boolean func_params = FALSE;
  332. X
  333. X/* This points to the list of parameters for the current function definition. */
  334. Xstatic ParameterList *cur_params;
  335. X
  336. X/* temporary string buffer */
  337. Xstatic char buf[MAX_TEXT_LENGTH];
  338. X
  339. X/* Table of typedef names */
  340. XSymbolTable *typedef_names;
  341. X%}
  342. X%%
  343. X
  344. Xprogram
  345. X    : /* empty */
  346. X    | translation_unit
  347. X    ;
  348. X
  349. Xtranslation_unit
  350. X    : external_declaration
  351. X    | translation_unit external_declaration
  352. X    ;
  353. X
  354. Xexternal_declaration
  355. X    : declaration
  356. X    | function_definition
  357. X    | T_EXTERN T_QUOTEC T_BRACES
  358. X    | error
  359. X    ;
  360. X
  361. Xdeclaration
  362. X    : declaration_specifiers ';'
  363. X    {
  364. X        free_decl_spec(&$1);
  365. X    }
  366. X    | declaration_specifiers init_declarator_list ';'
  367. X    {
  368. X        if (func_params) {
  369. X        set_param_types(cur_params, &$1, &$2);
  370. X        } else {
  371. X        output_declarations(&$1, &$2);
  372. X        }
  373. X        free_decl_spec(&$1);
  374. X        free_decl_list(&$2);
  375. X    }
  376. X    | T_TYPEDEF declaration_specifiers declarator_list ';'
  377. X    {
  378. X        free_decl_spec(&$2);
  379. X    }
  380. X    ;
  381. X
  382. Xdeclarator_list
  383. X    : declarator
  384. X    {
  385. X        new_symbol(typedef_names, $1.name);
  386. X        free_declarator(&$1);
  387. X    }
  388. X    | declarator_list ',' declarator
  389. X    {
  390. X        new_symbol(typedef_names, $3.name);
  391. X        free_declarator(&$3);
  392. X    }
  393. X    ;
  394. X
  395. Xfunction_definition
  396. X    : declaration_specifiers declarator
  397. X    {
  398. X        if ($2.func_def == FUNC_NONE) {
  399. X        yyerror("syntax error");
  400. X        YYERROR;
  401. X        }
  402. X        cur_params = &($2.params);
  403. X        func_params = TRUE;
  404. X    }
  405. X      opt_declaration_list T_BRACES
  406. X    {
  407. X        func_params = FALSE;
  408. X
  409. X        output_prototype(&$1, &$2);
  410. X        free_decl_spec(&$1);
  411. X        free_declarator(&$2);
  412. X    }
  413. X    | declarator
  414. X    {
  415. X        if ($1.func_def == FUNC_NONE) {
  416. X        yyerror("syntax error");
  417. X        YYERROR;
  418. X        }
  419. X        cur_params = &($1.params);
  420. X        func_params = TRUE;
  421. X    }
  422. X      opt_declaration_list T_BRACES
  423. X    {
  424. X        DeclSpec    decl_spec;
  425. X
  426. X        func_params = FALSE;
  427. X
  428. X        new_decl_spec(&decl_spec, "int", DE_EXTERN);
  429. X        output_prototype(&decl_spec, &$1);
  430. X        free_decl_spec(&decl_spec);
  431. X        free_declarator(&$1);
  432. X    }
  433. X    ;
  434. X
  435. Xopt_declaration_list
  436. X    : /* empty */
  437. X    | declaration_list
  438. X    ;
  439. X
  440. Xdeclaration_list
  441. X    : declaration
  442. X    | declaration_list declaration
  443. X    ;
  444. X
  445. Xdeclaration_specifiers
  446. X    : declaration_specifier
  447. X    | declaration_specifiers declaration_specifier
  448. X    {
  449. X        join_decl_specs(&$$, &$1, &$2);
  450. X    }
  451. X    ;
  452. X
  453. Xdeclaration_specifier
  454. X    : storage_class
  455. X    | type_specifier
  456. X    | type_qualifier
  457. X    ;
  458. X
  459. Xstorage_class
  460. X    : T_AUTO
  461. X    {
  462. X        new_decl_spec(&$$, "auto", DE_JUNK);
  463. X    }
  464. X    | T_EXTERN
  465. X    {
  466. X        new_decl_spec(&$$, "extern", DE_JUNK);
  467. X    }
  468. X    | T_REGISTER
  469. X    {
  470. X        new_decl_spec(&$$, "register", DE_JUNK);
  471. X    }
  472. X    | T_STATIC
  473. X    {
  474. X        new_decl_spec(&$$, "static", DE_STATIC);
  475. X    }
  476. X    | T_INLINE
  477. X    {
  478. X        new_decl_spec(&$$, "inline", DE_JUNK);
  479. X    }
  480. X    ;
  481. X
  482. Xtype_specifier
  483. X    : T_CHAR
  484. X    {
  485. X        new_decl_spec(&$$, "char", DE_EXTERN);
  486. X    }
  487. X    | T_DOUBLE
  488. X    {
  489. X        new_decl_spec(&$$, "double", DE_EXTERN);
  490. X    }
  491. X    | T_FLOAT
  492. X    {
  493. X        new_decl_spec(&$$, "float", DE_EXTERN);
  494. X    }
  495. X    | T_INT
  496. X    {
  497. X        new_decl_spec(&$$, "int", DE_EXTERN);
  498. X    }
  499. X    | T_LONG
  500. X    {
  501. X        new_decl_spec(&$$, "long", DE_EXTERN);
  502. X    }
  503. X    | T_SHORT
  504. X    {
  505. X        new_decl_spec(&$$, "short", DE_EXTERN);
  506. X    }
  507. X    | T_SIGNED
  508. X    {
  509. X        new_decl_spec(&$$, "signed", DE_EXTERN);
  510. X    }
  511. X    | T_UNSIGNED
  512. X    {
  513. X        new_decl_spec(&$$, "unsigned", DE_EXTERN);
  514. X    }
  515. X    | T_VOID
  516. X    {
  517. X        new_decl_spec(&$$, "void", DE_EXTERN);
  518. X    }
  519. X    | struct_or_union_specifier
  520. X    | enum_specifier
  521. X    | T_TYPEDEF_NAME
  522. X    {
  523. X        new_decl_spec(&$$, $1, DE_EXTERN);
  524. X    }
  525. X    ;
  526. X
  527. Xtype_qualifier
  528. X    : T_CONST
  529. X    {
  530. X        new_decl_spec(&$$, "const", DE_EXTERN);
  531. X    }
  532. X    | T_VOLATILE
  533. X    {
  534. X        new_decl_spec(&$$, "volatile", DE_EXTERN);
  535. X    }
  536. X    | T_CDECL
  537. X    {
  538. X        new_decl_spec(&$$, "cdecl", DE_EXTERN);
  539. X    }
  540. X    | T_INTERRUPT
  541. X    {
  542. X        new_decl_spec(&$$, "interrupt", DE_EXTERN);
  543. X    }
  544. X    | T_FAR
  545. X    {
  546. X        new_decl_spec(&$$, "far", DE_EXTERN);
  547. X    }
  548. X    | T_HUGE
  549. X    {
  550. X        new_decl_spec(&$$, "huge", DE_EXTERN);
  551. X    }
  552. X    | T_NEAR
  553. X    {
  554. X        new_decl_spec(&$$, "near", DE_EXTERN);
  555. X    }
  556. X    | T_PASCAL
  557. X    {
  558. X        new_decl_spec(&$$, "pascal", DE_EXTERN);
  559. X    }
  560. X    ;
  561. X
  562. Xstruct_or_union_specifier
  563. X    : struct_or_union any_id T_BRACES
  564. X    {
  565. X        sprintf(buf, "%s %s {}", $1, $2);
  566. X        new_decl_spec(&$$, buf, DE_EXTERN);
  567. X    }
  568. X    | struct_or_union T_BRACES
  569. X    {
  570. X        sprintf(buf, "%s {}", $1);
  571. X        new_decl_spec(&$$, buf, DE_EXTERN);
  572. X    }
  573. X    | struct_or_union any_id
  574. X    {
  575. X        sprintf(buf, "%s %s", $1, $2);
  576. X        new_decl_spec(&$$, buf, DE_EXTERN);
  577. X    }
  578. X    ;
  579. X
  580. Xstruct_or_union
  581. X    : T_STRUCT
  582. X    {
  583. X        strcpy($$, "struct");
  584. X    }
  585. X    | T_UNION
  586. X    {
  587. X        strcpy($$, "union");
  588. X    }
  589. X    ;
  590. X
  591. Xinit_declarator_list
  592. X    : init_declarator
  593. X    {
  594. X        new_decl_list(&$$, &$1);
  595. X    }
  596. X    | init_declarator_list ',' init_declarator
  597. X    {
  598. X        add_decl_list(&$$, &$1, &$3);
  599. X    }
  600. X    ;
  601. X
  602. Xinit_declarator
  603. X    : declarator
  604. X    | declarator T_INITIALIZER
  605. X    ;
  606. X
  607. Xenum_specifier
  608. X    : T_ENUM any_id T_BRACES
  609. X    {
  610. X        sprintf(buf, "enum %s {}", $2);
  611. X        new_decl_spec(&$$, buf, DE_EXTERN);
  612. X    }
  613. X    | T_ENUM T_BRACES
  614. X    {
  615. X        new_decl_spec(&$$, "enum {}", DE_EXTERN);
  616. X    }
  617. X    | T_ENUM any_id
  618. X    {
  619. X        sprintf(buf, "enum %s", $2);
  620. X        new_decl_spec(&$$, buf, DE_EXTERN);
  621. X    }
  622. X    ;
  623. X
  624. Xany_id
  625. X    : T_IDENTIFIER
  626. X    | T_TYPEDEF_NAME
  627. X    ;
  628. X
  629. Xdeclarator
  630. X    : pointer direct_declarator
  631. X    {
  632. X        sprintf(buf, "%s%s", $1, $2.text);
  633. X        $$ = $2;
  634. X        $$.text = strdup(buf);
  635. X        free($2.text);
  636. X    }
  637. X    | direct_declarator
  638. X    ;
  639. X
  640. Xdirect_declarator
  641. X    : T_IDENTIFIER
  642. X    {
  643. X        new_declarator(&$$, $1, $1);
  644. X    }
  645. X    | '(' declarator ')'
  646. X    {
  647. X        sprintf(buf, "(%s)", $2.text);
  648. X        $$ = $2;
  649. X        $$.text = strdup(buf);
  650. X        free($2.text);
  651. X    }
  652. X    | direct_declarator T_BRACKETS
  653. X    {
  654. X        sprintf(buf, "%s%s", $1.text, $2);
  655. X        $$ = $1;
  656. X        $$.text = strdup(buf);
  657. X        free($1.text);
  658. X    }
  659. X    | direct_declarator '(' parameter_type_list ')'
  660. X    {
  661. X        sprintf(buf, "%s()", $1.text);
  662. X        $$ = $1;
  663. X        $$.text = strdup(buf);
  664. X        $$.func_def = FUNC_ANSI;
  665. X        $$.params = $3;
  666. X        free($1.text);
  667. X    }
  668. X    | direct_declarator '(' opt_identifier_list ')'
  669. X    {
  670. X        sprintf(buf, "%s()", $1.text);
  671. X        $$ = $1;
  672. X        $$.text = strdup(buf);
  673. X        $$.func_def = FUNC_TRADITIONAL;
  674. X        if ($3.first != NULL) {
  675. X        $$.params = $3;
  676. X        }
  677. X        free($1.text);
  678. X    }
  679. X    ;
  680. X
  681. Xpointer
  682. X    : '*' type_qualifiers
  683. X    {
  684. X        sprintf($$, "*%s", $2);
  685. X    }
  686. X    | '*' type_qualifiers pointer
  687. X    {
  688. X        sprintf($$, "*%s%s", $2, $3);
  689. X    }
  690. X    ;
  691. X
  692. Xtype_qualifiers
  693. X    : /* empty */
  694. X    {
  695. X        strcpy($$, "");
  696. X    }
  697. X    | type_qualifiers type_qualifier
  698. X    {
  699. X        sprintf($$, "%s %s ", $1, $2.text);
  700. X        free($2.text);
  701. X    }
  702. X    ;
  703. X
  704. Xparameter_type_list
  705. X    : parameter_list
  706. X    | parameter_list ',' T_ELLIPSIS
  707. X    {
  708. X        add_ident_list(&$$, &$1, "...");
  709. X    }
  710. X    ;
  711. X
  712. Xparameter_list
  713. X    : parameter_declaration
  714. X    {
  715. X        new_param_list(&$$, &$1);
  716. X    }
  717. X    | parameter_list ',' parameter_declaration
  718. X    {
  719. X        add_param_list(&$$, &$1, &$3);
  720. X    }
  721. X    ;
  722. X
  723. Xparameter_declaration
  724. X    : declaration_specifiers declarator
  725. X    {
  726. X        new_parameter(&$$, &$1, &$2);
  727. X    }
  728. X    | declaration_specifiers abs_declarator
  729. X    {
  730. X        new_parameter(&$$, &$1, &$2);
  731. X    }
  732. X    | declaration_specifiers
  733. X    {
  734. X        new_parameter(&$$, &$1, NULL);
  735. X    }
  736. X    ;
  737. X
  738. Xopt_identifier_list
  739. X    : /* empty */
  740. X    {
  741. X        new_ident_list(&$$);
  742. X    }
  743. X    | identifier_list
  744. X    ;
  745. X
  746. Xidentifier_list
  747. X    : T_IDENTIFIER
  748. X    {
  749. X        new_ident_list(&$$);
  750. X        add_ident_list(&$$, &$$, $1);
  751. X    }
  752. X    | identifier_list ',' T_IDENTIFIER
  753. X    {
  754. X        add_ident_list(&$$, &$1, $3);
  755. X    }
  756. X    ;
  757. X
  758. Xabs_declarator
  759. X    : pointer
  760. X    {
  761. X        new_declarator(&$$, $1, "");
  762. X    }
  763. X    | pointer direct_abs_declarator
  764. X    {
  765. X        sprintf(buf, "%s%s", $1, $2.text);
  766. X        $$ = $2;
  767. X        $$.text = strdup(buf);
  768. X        free($2.text);
  769. X    }
  770. X    | direct_abs_declarator
  771. X    ;
  772. X
  773. Xdirect_abs_declarator
  774. X    : '(' abs_declarator ')'
  775. X    {
  776. X        sprintf(buf, "(%s)", $2.text);
  777. X        $$ = $2;
  778. X        $$.text = strdup(buf);
  779. X        free($2.text);
  780. X    }
  781. X    | direct_abs_declarator T_BRACKETS
  782. X    {
  783. X        sprintf(buf, "%s%s", $1.text, $2);
  784. X        $$ = $1;
  785. X        $$.text = strdup(buf);
  786. X        free($1.text);
  787. X    }
  788. X    | T_BRACKETS
  789. X    {
  790. X        new_declarator(&$$, $1, "");
  791. X    }
  792. X    | direct_abs_declarator '(' parameter_type_list ')'
  793. X    {
  794. X        sprintf(buf, "%s(%%s)", $1.text);
  795. X        $$ = $1;
  796. X        $$.text = strdup(buf);
  797. X        free($1.text);
  798. X    }
  799. X    | direct_abs_declarator '(' ')'
  800. X    {
  801. X        sprintf(buf, "%s()", $1.text);
  802. X        $$ = $1;
  803. X        $$.text = strdup(buf);
  804. X        free($1.text);
  805. X    }
  806. X    | '(' parameter_type_list ')'
  807. X    {
  808. X        new_declarator(&$$, "()", "");
  809. X    }
  810. X    | '(' ')'
  811. X    {
  812. X        new_declarator(&$$, "()", "");
  813. X    }
  814. X    ;
  815. X
  816. X%%
  817. X#ifdef MSDOS
  818. X#include "lex_yy.c"
  819. X#else
  820. X#include "lex.yy.c"
  821. X#endif
  822. X
  823. Xyyerror (msg)
  824. Xchar *msg;
  825. X{
  826. X    output_error();
  827. X    fprintf(stderr, "%s\n", msg);
  828. X}
  829. X
  830. Xvoid
  831. Xparse_file ()
  832. X{
  833. X    typedef_names = create_symbol_table();
  834. X    yyparse();
  835. X}
  836. END_OF_grammar.y
  837. if test 10073 -ne `wc -c <grammar.y`; then
  838.     echo shar: \"grammar.y\" unpacked with wrong size!
  839. fi
  840. # end of overwriting check
  841. fi
  842. if test -f config.h -a "${1}" != "-c" ; then 
  843.   echo shar: Will not over-write existing file \"config.h\"
  844. else
  845. echo shar: Extracting \"config.h\" \(640 characters\)
  846. sed "s/^X//" >config.h <<'END_OF_config.h'
  847. X/* $Id: config.h 2.1 91/02/28 11:16:12 cthuang Exp $
  848. X *
  849. X * cproto configuration and system dependencies
  850. X */
  851. X
  852. X/* maximum include file nesting */
  853. X#define MAX_INC_DEPTH 15
  854. X
  855. X/* maximum number of include directories */
  856. X#define MAX_INC_DIR 15
  857. X
  858. X/* maximum number of characters in a text buffer */
  859. X#define MAX_TEXT_LENGTH    256
  860. X
  861. X#ifdef __TURBOC__
  862. X#include <alloc.h>
  863. X#endif
  864. X
  865. X#ifdef M_I86
  866. X#include <malloc.h>
  867. X#endif
  868. X
  869. X#ifndef MSDOS
  870. Xextern char *malloc();
  871. X#endif
  872. X
  873. X#if defined(SYSV) || defined(MSDOS)
  874. X#include <string.h>
  875. X#define index strchr
  876. X#define rindex strrchr
  877. X#else
  878. X#include <strings.h>
  879. X#endif
  880. X
  881. X#ifndef MSDOS
  882. Xextern char *strdup(), *strstr();
  883. X#endif
  884. END_OF_config.h
  885. if test 640 -ne `wc -c <config.h`; then
  886.     echo shar: \"config.h\" unpacked with wrong size!
  887. fi
  888. # end of overwriting check
  889. fi
  890. if test -f cproto.h -a "${1}" != "-c" ; then 
  891.   echo shar: Will not over-write existing file \"cproto.h\"
  892. else
  893. echo shar: Extracting \"cproto.h\" \(2851 characters\)
  894. sed "s/^X//" >cproto.h <<'END_OF_cproto.h'
  895. X/* $Id: cproto.h 2.1 91/02/28 11:16:14 cthuang Exp $
  896. X *
  897. X * Definitions for C language prototype generator
  898. X */
  899. X#include "config.h"
  900. X#include "symbol.h"
  901. X
  902. X/* Boolean type */
  903. Xtypedef char boolean;
  904. X#define FALSE    0
  905. X#define TRUE    1
  906. X
  907. X/* This is a list of function parameters. */
  908. Xtypedef struct _parameter_list {
  909. X    struct _parameter    *first;    /* pointer to first parameter in list */
  910. X    struct _parameter    *last;  /* pointer to last parameter in list */  
  911. X} ParameterList;
  912. X
  913. X/* Declaration specifier flags */
  914. X#define DE_EXTERN    0    /* default: external declaration */
  915. X#define DE_STATIC    1    /* visible only in current file */
  916. X#define DE_JUNK        2    /* we're not interested in this declaration */
  917. X
  918. X/* This structure stores information about a declaration specifier. */
  919. Xtypedef struct _decl_spec {
  920. X    unsigned short    flags;    /* flags defined above */
  921. X    char        *text;    /* source text */
  922. X} DeclSpec;
  923. X
  924. X/* Styles of function definitions */
  925. Xtypedef enum {
  926. X    FUNC_NONE,        /* not a function definition */
  927. X    FUNC_TRADITIONAL,    /* traditional style */
  928. X    FUNC_ANSI        /* ANSI style */
  929. X} FuncDefType;
  930. X
  931. X/* This structure stores information about a declarator. */
  932. Xtypedef struct _declarator {
  933. X    char        *name;        /* name of variable or function */
  934. X    char        *text;        /* source text */
  935. X    FuncDefType        func_def;    /* style of function definition */
  936. X    ParameterList    params;        /* function parameters */
  937. X    struct _declarator    *next;        /* next declarator in list */
  938. X} Declarator;
  939. X
  940. X/* This is a list of declarators. */
  941. Xtypedef struct _declarator_list {
  942. X    Declarator        *first;    /* pointer to first declarator in list */
  943. X    Declarator        *last;  /* pointer to last declarator in list */  
  944. X} DeclaratorList;
  945. X
  946. X/* This structure stores information about a function parameter. */
  947. Xtypedef struct _parameter {
  948. X    DeclSpec        decl_spec;
  949. X    Declarator        declarator;
  950. X    struct _parameter    *next;        /* next parameter in list */
  951. X} Parameter;
  952. X
  953. X/* parser stack entry type */
  954. Xtypedef union {
  955. X    char        text[MAX_TEXT_LENGTH];
  956. X    DeclSpec        decl_spec;
  957. X    Parameter        parameter;
  958. X    ParameterList    param_list;
  959. X    Declarator        declarator;
  960. X    DeclaratorList    decl_list;
  961. X} yystype;
  962. X
  963. X#define YYSTYPE yystype
  964. X
  965. X/* Prototype styles */
  966. X#define PROTO_NONE        0
  967. X#define PROTO_TRADITIONAL    1
  968. X#define PROTO_ABSTRACT        2
  969. X#define PROTO_ANSI        3
  970. X#define PROTO_MACRO        4
  971. X
  972. X/* Program options */
  973. Xextern boolean extern_out;
  974. Xextern boolean static_out;
  975. Xextern boolean variables_out;
  976. Xextern boolean promote_param;
  977. Xextern int proto_style;
  978. Xextern boolean define_macro;
  979. Xextern char *macro_name;
  980. Xextern char *decl_spec_prefix, *declarator_prefix, *declarator_suffix;
  981. Xextern char *first_param_prefix, *middle_param_prefix, *last_param_suffix;
  982. Xextern int num_inc_dir;
  983. Xextern char *inc_dir[];
  984. X
  985. X/* Global declarations */
  986. Xextern int line_num;
  987. Xextern char cur_file[];
  988. Xextern SymbolTable *typedef_names;
  989. Xextern void output_error();
  990. Xextern void parse_file();
  991. END_OF_cproto.h
  992. if test 2851 -ne `wc -c <cproto.h`; then
  993.     echo shar: \"cproto.h\" unpacked with wrong size!
  994. fi
  995. # end of overwriting check
  996. fi
  997. if test -f patchlev.h -a "${1}" != "-c" ; then 
  998.   echo shar: Will not over-write existing file \"patchlev.h\"
  999. else
  1000. echo shar: Extracting \"patchlev.h\" \(21 characters\)
  1001. sed "s/^X//" >patchlev.h <<'END_OF_patchlev.h'
  1002. X#define PATCHLEVEL 0
  1003. END_OF_patchlev.h
  1004. if test 21 -ne `wc -c <patchlev.h`; then
  1005.     echo shar: \"patchlev.h\" unpacked with wrong size!
  1006. fi
  1007. # end of overwriting check
  1008. fi
  1009. if test -f semantic.h -a "${1}" != "-c" ; then 
  1010.   echo shar: Will not over-write existing file \"semantic.h\"
  1011. else
  1012. echo shar: Extracting \"semantic.h\" \(1610 characters\)
  1013. sed "s/^X//" >semantic.h <<'END_OF_semantic.h'
  1014. X/* $Id: semantic.h 2.1 91/02/28 11:16:19 cthuang Exp $
  1015. X *
  1016. X * Declarations for semantics action routines
  1017. X */
  1018. X
  1019. Xextern boolean is_typedef_name(/*
  1020. X    char *name
  1021. X    */);
  1022. Xextern void new_decl_spec(/*
  1023. X    DeclSpec *decl_spec,
  1024. X    char *text,
  1025. X    unsigned short flags
  1026. X    */);
  1027. Xextern void join_decl_specs(/*
  1028. X    DeclSpec *result,
  1029. X    DeclSpec *a,
  1030. X    DeclSpec *b
  1031. X    */);
  1032. Xextern void free_decl_spec(/*
  1033. X    DeclSpec *decl_spec
  1034. X    */);
  1035. Xextern void new_parameter(/*
  1036. X    Parameter *param,
  1037. X    DeclSpec *decl_spec,
  1038. X    Declarator *declarator
  1039. X    */);
  1040. Xextern void free_parameter(/*
  1041. X    Parameter *param
  1042. X    */);
  1043. Xextern void new_param_list(/*
  1044. X    ParameterList *param_list,
  1045. X    Parameter *param
  1046. X    */);
  1047. Xextern void add_param_list(/*
  1048. X    ParameterList *to,
  1049. X    ParameterList *from,
  1050. X    Parameter *param
  1051. X    */);
  1052. Xextern void free_param_list(/*
  1053. X    ParameterList *param_list
  1054. X    */);
  1055. Xextern void new_ident_list(/*
  1056. X    ParameterList *param_list
  1057. X    */);
  1058. Xextern void add_ident_list(/*
  1059. X    ParameterList *to,
  1060. X    ParameterList *from,
  1061. X    char *name
  1062. X    */);
  1063. Xextern void new_declarator(/*
  1064. X    Declarator *d,
  1065. X    char *name,
  1066. X    char *text
  1067. X    */);
  1068. Xextern void free_declarator(/*
  1069. X    Declarator *d
  1070. X    */);
  1071. Xextern void new_decl_list(/*
  1072. X    DeclaratorList *decl_list,
  1073. X    Declarator *declarator
  1074. X    */);
  1075. Xextern void add_decl_list(/*
  1076. X    DeclaratorList *to,
  1077. X    DeclaratorList *from,
  1078. X    Declarator *declarator
  1079. X    */);
  1080. Xextern void free_decl_list(/*
  1081. X    DeclaratorList *decl_list
  1082. X    */);
  1083. Xextern void set_param_types(/*
  1084. X    ParameterList *params,
  1085. X    DeclSpec *decl_spec,
  1086. X    DeclaratorList *declarators
  1087. X    */);
  1088. Xextern void output_declarations(/*
  1089. X    DeclSpec *decl_spec,
  1090. X    DeclaratorList *decl_list
  1091. X    */);
  1092. Xextern void output_prototype(/*
  1093. X    DeclSpec *decl_spec,
  1094. X    Declarator *declarator
  1095. X    */);
  1096. END_OF_semantic.h
  1097. if test 1610 -ne `wc -c <semantic.h`; then
  1098.     echo shar: \"semantic.h\" unpacked with wrong size!
  1099. fi
  1100. # end of overwriting check
  1101. fi
  1102. if test -f symbol.h -a "${1}" != "-c" ; then 
  1103.   echo shar: Will not over-write existing file \"symbol.h\"
  1104. else
  1105. echo shar: Extracting \"symbol.h\" \(587 characters\)
  1106. sed "s/^X//" >symbol.h <<'END_OF_symbol.h'
  1107. X/* $Id: symbol.h 2.1 91/02/28 11:16:22 cthuang Exp $
  1108. X *
  1109. X * Definitions for a symbol table
  1110. X */
  1111. X#ifndef _SYMBOL_H
  1112. X#define _SYMBOL_H
  1113. X
  1114. Xtypedef struct _symbol {
  1115. X    struct _symbol *next;    /* next symbol in list */
  1116. X    char *name;        /* name of symbol */
  1117. X} Symbol;
  1118. X
  1119. X/* hash table length */
  1120. X#define SYM_MAX_HASH 256
  1121. X
  1122. Xtypedef struct _symbol_table {
  1123. X    Symbol *bucket[SYM_MAX_HASH];    /* hash buckets */
  1124. X} SymbolTable;
  1125. X
  1126. Xextern SymbolTable *create_symbol_table();    /* Create symbol table */
  1127. Xextern Symbol *find_symbol();            /* Lookup symbol name */
  1128. Xextern Symbol *new_symbol();            /* Define new symbol */
  1129. X
  1130. X#endif
  1131. END_OF_symbol.h
  1132. if test 587 -ne `wc -c <symbol.h`; then
  1133.     echo shar: \"symbol.h\" unpacked with wrong size!
  1134. fi
  1135. # end of overwriting check
  1136. fi
  1137. if test -f cproto.c -a "${1}" != "-c" ; then 
  1138.   echo shar: Will not over-write existing file \"cproto.c\"
  1139. else
  1140. echo shar: Extracting \"cproto.c\" \(7676 characters\)
  1141. sed "s/^X//" >cproto.c <<'END_OF_cproto.c'
  1142. X/* $Id: cproto.c 2.1 91/03/25 11:40:34 cthuang Exp $
  1143. X *
  1144. X * C prototype generator
  1145. X * Reads C source code and outputs ANSI C function prototypes.
  1146. X */
  1147. X#ifndef lint
  1148. Xstatic char *rcsid = "$Id: cproto.c 2.1 91/03/25 11:40:34 cthuang Exp $";
  1149. X#endif
  1150. X#include <stdio.h>
  1151. X#include <ctype.h>
  1152. X#include "cproto.h"
  1153. X#include "patchlev.h"
  1154. X
  1155. X/* C preprocessor */
  1156. X#ifndef CPP
  1157. X#define CPP "/lib/cpp"
  1158. X#endif
  1159. X
  1160. X/* getopt declarations */
  1161. Xextern int getopt();
  1162. Xextern char *optarg;
  1163. Xextern int optind;
  1164. X
  1165. X/* lex declarations */
  1166. Xextern FILE *yyin;    /* lex input stream */
  1167. X
  1168. X/* Name of the program */
  1169. Xstatic char *progname = "cproto";
  1170. X
  1171. X/* Program options */
  1172. X
  1173. X/* TRUE if "extern" should appear on external declarations. */
  1174. Xboolean extern_out = FALSE;
  1175. X
  1176. X/* TRUE if static declarations are also output. */
  1177. Xboolean static_out = FALSE;
  1178. X
  1179. X/* TRUE if variable declarations are output. */
  1180. Xboolean variables_out = FALSE;
  1181. X
  1182. X/* TRUE if formal parameter promotion is enabled. */
  1183. Xboolean promote_param = TRUE;
  1184. X
  1185. X/* Style of function prototype generated */
  1186. Xint proto_style = PROTO_ANSI;
  1187. X
  1188. X/* Name of macro to guard prototypes */
  1189. Xchar *macro_name = "P_";
  1190. X
  1191. X/* TRUE if prototype macro definition is output. */
  1192. Xboolean define_macro = TRUE;
  1193. X
  1194. X/* String output before prototype declaration specifiers */
  1195. Xchar *decl_spec_prefix = "";
  1196. X
  1197. X/* String output before prototype declarator */
  1198. Xchar *declarator_prefix = " ";
  1199. X
  1200. X/* String output after prototype declarator */
  1201. Xchar *declarator_suffix = "";
  1202. X
  1203. X/* String output before the first parameter in a function prototype */
  1204. Xchar *first_param_prefix = "";
  1205. X
  1206. X/* String output before each subsequent parameter in a function prototype */
  1207. Xchar *middle_param_prefix = " ";
  1208. X
  1209. X/* String output after the last parameter in a function prototype */
  1210. Xchar *last_param_suffix = "";
  1211. X
  1212. X/* Include file directories */
  1213. X#ifdef MSDOS
  1214. Xint num_inc_dir = 1;
  1215. Xchar *inc_dir[MAX_INC_DIR] = { ".\\" };
  1216. X#else
  1217. Xint num_inc_dir = 2;
  1218. Xchar *inc_dir[MAX_INC_DIR] = { "./", "/usr/include/" };
  1219. X#endif
  1220. X
  1221. X/* Output an error message along with the current line number in the
  1222. X * source file.
  1223. X */
  1224. Xvoid
  1225. Xoutput_error ()
  1226. X{
  1227. X    fprintf(stderr, "\"%s\", line %d: ", cur_file, line_num);
  1228. X}
  1229. X
  1230. X/* Replace any character escape sequences in a string with the actual
  1231. X * characters.  Return a pointer to malloc'ed memory containing the result.
  1232. X * This function knows only a few escape sequences.
  1233. X */
  1234. Xstatic char *
  1235. Xescape_string (src)
  1236. Xchar *src;
  1237. X{
  1238. X    char *result, *get, *put;
  1239. X
  1240. X    result = strdup(src);
  1241. X    put = result;
  1242. X    get = src;
  1243. X    while (*get != '\0') {
  1244. X    if (*get == '\\') {
  1245. X        switch (*(++get)) {
  1246. X        case 'n':
  1247. X        *put++ = '\n';
  1248. X        ++get;
  1249. X        break;
  1250. X        case 't':
  1251. X        *put++ = '\t';
  1252. X        ++get;
  1253. X        break;
  1254. X        default:
  1255. X        if (*get != '\0')
  1256. X            *put++ = *get++;
  1257. X        }
  1258. X    } else {
  1259. X        *put++ = *get++;
  1260. X    }
  1261. X    }
  1262. X    *put = *get;
  1263. X    return result;
  1264. X}
  1265. X
  1266. X/* Append a path name separator to the end of the string if it doesn't
  1267. X * end with one already.  Allocate storage for the result and return
  1268. X * a pointer to it.
  1269. X */
  1270. Xstatic char *
  1271. Xadd_path_sep (s)
  1272. Xchar *s;
  1273. X{
  1274. X    char *result, ch;
  1275. X    int n;
  1276. X
  1277. X    n = strlen(s);
  1278. X    result = malloc(n+2);
  1279. X    strcpy(result, s);
  1280. X    ch = result[n-1];
  1281. X    if (ch != '/' && ch != '\\') {
  1282. X    result[n] = '/';
  1283. X    result[n+1] = '\0';
  1284. X    }
  1285. X    return result;
  1286. X}
  1287. X
  1288. X/* Output usage message and exit.
  1289. X */
  1290. Xstatic void
  1291. Xusage ()
  1292. X{
  1293. X    fprintf(stderr,
  1294. X    "usage: %s [ option ... ] [ file ... ]\n", progname);
  1295. X    fputs("  -e      output \"extern\" keyword before global declarations\n",
  1296. X    stderr);
  1297. X    fputs("  -f n    select function prototype style (0 to 4)\n", stderr);
  1298. X    fputs("  -p      disable prototype promotion\n", stderr);
  1299. X    fputs("  -s      output static declarations\n", stderr);
  1300. X    fputs("  -v      output variable declarations\n", stderr);
  1301. X    fputs("  -m nam  set name of macro guarding prototypes\n", stderr);
  1302. X    fputs("  -d      omit prototype macro definition\n", stderr);
  1303. X    fputs("  -D name[=value]\n", stderr);
  1304. X    fputs("  -U name\n", stderr);
  1305. X    fputs("  -I directory\n", stderr);
  1306. X    fputs("          C preprocessor options\n", stderr);
  1307. X    fputs(
  1308. X    "  -F fmt  set prototype template in the form \"int main (a, b)\"\n",
  1309. X    stderr);
  1310. X    fputs("  -V      print version information\n", stderr);
  1311. X    exit(1);
  1312. X}
  1313. X
  1314. Xmain (argc, argv)
  1315. Xint argc;
  1316. Xchar **argv;
  1317. X{
  1318. X    int i, c, n;
  1319. X    char *s, *cpp_cmd, tmp[MAX_TEXT_LENGTH];
  1320. X#ifndef MSDOS
  1321. X    char *cmd;
  1322. X#endif
  1323. X
  1324. X    /* Allocate buffer for C preprocessor command line. */
  1325. X    n = strlen(CPP) + 1;
  1326. X    for (i = 0; i < argc; ++i) {
  1327. X    n += strlen(argv[i]) + 1;
  1328. X    }
  1329. X    cpp_cmd = malloc(n);
  1330. X    strcpy(cpp_cmd, CPP);
  1331. X#ifndef MSDOS
  1332. X    cmd = malloc(n);
  1333. X#endif
  1334. X
  1335. X    /* Scan command line options. */
  1336. X    while ((c = getopt(argc, argv, "D:deF:f:I:m:psU:Vv")) != EOF) {
  1337. X    switch (c) {
  1338. X    case 'I':
  1339. X        if (num_inc_dir < MAX_INC_DIR) {
  1340. X        inc_dir[num_inc_dir++] = add_path_sep(optarg);
  1341. X        } else {
  1342. X        fprintf(stderr, "%s: too many include directories\n",
  1343. X            progname);
  1344. X        }
  1345. X    case 'D':
  1346. X    case 'U':
  1347. X        sprintf(tmp, " -%c%s", c, optarg);
  1348. X        strcat(cpp_cmd, tmp);
  1349. X        break;
  1350. X    case 'd':
  1351. X        define_macro = FALSE;
  1352. X        break;
  1353. X    case 'e':
  1354. X        extern_out = TRUE;
  1355. X        break;
  1356. X    case 'F':
  1357. X        s = escape_string(optarg);
  1358. X
  1359. X        decl_spec_prefix = s;
  1360. X        while (*s != '\0' && isascii(*s) && !isalnum(*s)) ++s;
  1361. X        if (*s == '\0') usage();
  1362. X        *s++ = '\0';
  1363. X        while (*s != '\0' && isascii(*s) && isalnum(*s)) ++s;
  1364. X        if (*s == '\0') usage();
  1365. X
  1366. X        declarator_prefix = s;
  1367. X        while (*s != '\0' && isascii(*s) && !isalnum(*s)) ++s;
  1368. X        if (*s == '\0') usage();
  1369. X        *s++ = '\0';
  1370. X        while (*s != '\0' && isascii(*s) && isalnum(*s)) ++s;
  1371. X        if (*s == '\0') usage();
  1372. X
  1373. X        declarator_suffix = s;
  1374. X        while (*s != '\0' && *s != '(') ++s;
  1375. X        if (*s == '\0') usage();
  1376. X        *s++ = '\0';
  1377. X
  1378. X        first_param_prefix = s;
  1379. X        while (*s != '\0' && isascii(*s) && !isalnum(*s)) ++s;
  1380. X        if (*s == '\0') usage();
  1381. X        *s++ = '\0';
  1382. X        while (*s != '\0' && *s != ',') ++s;
  1383. X        if (*s == '\0') usage();
  1384. X
  1385. X        middle_param_prefix = ++s;
  1386. X        while (*s != '\0' && isascii(*s) && !isalnum(*s)) ++s;
  1387. X        if (*s == '\0') usage();
  1388. X        *s++ = '\0';
  1389. X        while (*s != '\0' && isascii(*s) && isalnum(*s)) ++s;
  1390. X        if (*s == '\0') usage();
  1391. X
  1392. X        last_param_suffix = s;
  1393. X        while (*s != '\0' && *s != ')') ++s;
  1394. X        *s = '\0';
  1395. X
  1396. X        break;
  1397. X    case 'f':
  1398. X        proto_style = atoi(optarg);
  1399. X        if (proto_style < 0 || proto_style > PROTO_MACRO)
  1400. X        proto_style = PROTO_ANSI;
  1401. X        break;
  1402. X    case 'm':
  1403. X        macro_name = optarg;
  1404. X        break;
  1405. X    case 'p':
  1406. X        promote_param = FALSE;
  1407. X        break;
  1408. X    case 's':
  1409. X        static_out = TRUE;
  1410. X        break;
  1411. X    case 'V':
  1412. X        fprintf(stderr, "%s patchlevel %d\n", rcsid, PATCHLEVEL);
  1413. X        break;
  1414. X    case 'v':
  1415. X        variables_out = TRUE;
  1416. X        break;
  1417. X    case '?':
  1418. X    default:
  1419. X        usage();
  1420. X    }
  1421. X    }
  1422. X
  1423. X    if (proto_style == PROTO_MACRO && define_macro) {
  1424. X    printf("#if defined(__STDC__) || defined(__cplusplus)\n");
  1425. X    printf("# define %s(s) s\n", macro_name);
  1426. X    printf("#else\n");
  1427. X    printf("# define %s(s) ()\n", macro_name);
  1428. X    printf("#endif\n\n");
  1429. X    }
  1430. X
  1431. X    if (optind == argc) {
  1432. X    printf("/* stdin */\n");
  1433. X    parse_file();
  1434. X    } else {
  1435. X    for (i = optind; i < argc; ++i) {
  1436. X#ifdef MSDOS
  1437. X        if (freopen(argv[i], "r", yyin) == NULL) {
  1438. X        fprintf(stderr, "%s: cannot open file %s\n", progname, argv[i]);
  1439. X        continue;
  1440. X        }
  1441. X#else
  1442. X        sprintf(cmd, "%s %s", cpp_cmd, argv[i]);
  1443. X        if ((yyin = popen(cmd, "r")) == NULL) {
  1444. X        fprintf(stderr, "%s: error running cpp\n", progname);
  1445. X        continue;
  1446. X        }
  1447. X#endif
  1448. X        strcpy(cur_file, argv[i]);
  1449. X        line_num = 1;
  1450. X        printf("/* %s */\n", cur_file);
  1451. X        parse_file();
  1452. X#ifdef MSDOS
  1453. X        fclose(yyin);
  1454. X#else
  1455. X        pclose(yyin);
  1456. X#endif
  1457. X    }
  1458. X    }
  1459. X
  1460. X    if (proto_style == PROTO_MACRO && define_macro) {
  1461. X    printf("\n#undef %s\n", macro_name);
  1462. X    }
  1463. X
  1464. X    return 0;
  1465. X}
  1466. END_OF_cproto.c
  1467. if test 7676 -ne `wc -c <cproto.c`; then
  1468.     echo shar: \"cproto.c\" unpacked with wrong size!
  1469. fi
  1470. # end of overwriting check
  1471. fi
  1472. if test -f semantic.c -a "${1}" != "-c" ; then 
  1473.   echo shar: Will not over-write existing file \"semantic.c\"
  1474. else
  1475. echo shar: Extracting \"semantic.c\" \(10639 characters\)
  1476. sed "s/^X//" >semantic.c <<'END_OF_semantic.c'
  1477. X/* $Id: semantic.c 2.1 91/03/25 11:40:31 cthuang Exp $
  1478. X *
  1479. X * C prototype generator
  1480. X * These routines implement the semantic actions executed by the yacc parser.
  1481. X */
  1482. X#include <stdio.h>
  1483. X#include "cproto.h"
  1484. X#include "semantic.h"
  1485. X
  1486. X/* Output a string to standard output. */
  1487. X#define put_string(s) fputs(s, stdout)
  1488. X
  1489. X/* Create a new string by joining two strings with a space between them.
  1490. X * Return a pointer to the resultant string or NULL if an error occurred.
  1491. X */
  1492. Xstatic char *
  1493. Xconcat_string (a, b)
  1494. Xchar *a, *b;
  1495. X{
  1496. X    char *result;
  1497. X
  1498. X    if ((result = malloc((unsigned)(strlen(a) + strlen(b) + 2))) != NULL) {
  1499. X    strcpy(result, a);
  1500. X    strcat(result, " ");
  1501. X    strcat(result, b);
  1502. X    }
  1503. X    return result;
  1504. X}
  1505. X
  1506. X/* Return TRUE if the given identifier is really a typedef name.
  1507. X * Search the symbol table for the identifier.
  1508. X */
  1509. Xboolean
  1510. Xis_typedef_name (name)
  1511. Xchar *name;
  1512. X{
  1513. X    return (boolean)(find_symbol(typedef_names, name) != NULL);
  1514. X}
  1515. X
  1516. X/* Initialize a new declaration specifier part.
  1517. X */
  1518. Xvoid
  1519. Xnew_decl_spec (decl_spec, text, flags)
  1520. XDeclSpec *decl_spec;
  1521. Xchar *text;
  1522. Xunsigned short flags;
  1523. X{
  1524. X    decl_spec->text = strdup(text);
  1525. X    decl_spec->flags = flags;
  1526. X}
  1527. X
  1528. X/* Append two declaration specifier parts together.
  1529. X */
  1530. Xvoid
  1531. Xjoin_decl_specs (result, a, b)
  1532. XDeclSpec *result, *a, *b;
  1533. X{
  1534. X    result->text = concat_string(a->text, b->text);
  1535. X    result->flags = a->flags | b->flags;
  1536. X    free(a->text);
  1537. X    free(b->text);
  1538. X}
  1539. X
  1540. X/* Free storage used by a declaration specifier part.
  1541. X */
  1542. Xvoid
  1543. Xfree_decl_spec (decl_spec)
  1544. XDeclSpec *decl_spec;
  1545. X{
  1546. X    free(decl_spec->text);
  1547. X}
  1548. X
  1549. X/* Initialize the parameter structure.
  1550. X */
  1551. Xvoid
  1552. Xnew_parameter (param, decl_spec, declarator)
  1553. XParameter *param;        /* pointer to structure to be initialized */
  1554. XDeclSpec *decl_spec;        /* declaration specifier structure */
  1555. XDeclarator *declarator;        /* declarator structure */
  1556. X{
  1557. X    if (decl_spec == NULL) {
  1558. X    new_decl_spec(&(param->decl_spec), "", DE_JUNK);
  1559. X    } else {
  1560. X    param->decl_spec = *decl_spec;
  1561. X    }
  1562. X
  1563. X    if (declarator == NULL) {
  1564. X    new_declarator(&(param->declarator), "", "");
  1565. X    } else {
  1566. X    param->declarator = *declarator;
  1567. X    }
  1568. X}
  1569. X
  1570. X/* Free the storage used by the parameter.
  1571. X */
  1572. Xvoid
  1573. Xfree_parameter (param)
  1574. XParameter *param;
  1575. X{
  1576. X    free_decl_spec(&(param->decl_spec));
  1577. X    free_declarator(&(param->declarator));
  1578. X}
  1579. X
  1580. X/* Initialize a list of function parameters.
  1581. X */
  1582. Xvoid
  1583. Xnew_param_list (param_list, param)
  1584. XParameterList *param_list;
  1585. XParameter *param;
  1586. X{
  1587. X    Parameter *p;
  1588. X
  1589. X    p = (Parameter *)malloc((unsigned)sizeof(Parameter));
  1590. X    *p = *param;
  1591. X    
  1592. X    param_list->first = param_list->last = p;
  1593. X    p->next = NULL;
  1594. X}
  1595. X
  1596. X/* Add the function parameter declaration to the list.
  1597. X */
  1598. Xvoid
  1599. Xadd_param_list (to, from, param)
  1600. XParameterList *to, *from;
  1601. XParameter *param;
  1602. X{
  1603. X    Parameter *p;
  1604. X
  1605. X    p = (Parameter *)malloc((unsigned)sizeof(Parameter));
  1606. X    *p = *param;
  1607. X
  1608. X    to->first = from->first;
  1609. X    from->last->next = p;
  1610. X    to->last = p;
  1611. X    p->next = NULL;
  1612. X}
  1613. X
  1614. X/* Free storage used by the elements in the function parameter list.
  1615. X */
  1616. Xvoid
  1617. Xfree_param_list (param_list)
  1618. XParameterList *param_list;
  1619. X{
  1620. X    Parameter *p, *next;
  1621. X
  1622. X    p = param_list->first;
  1623. X    while (p != NULL) {
  1624. X    next = p->next;
  1625. X    free_parameter(p);
  1626. X    free(p);
  1627. X    p = next;
  1628. X    }
  1629. X}
  1630. X
  1631. X/* Initialize an empty list of function parameter names.
  1632. X */
  1633. Xvoid
  1634. Xnew_ident_list (param_list)
  1635. XParameterList *param_list;
  1636. X{
  1637. X    param_list->first = param_list->last = NULL;
  1638. X}
  1639. X
  1640. X/* Add an item to the list of function parameter declarations but set only
  1641. X * the parameter name field.
  1642. X */
  1643. Xvoid
  1644. Xadd_ident_list (to, from, name)
  1645. XParameterList *to, *from;
  1646. Xchar *name;
  1647. X{
  1648. X    Parameter *p;
  1649. X    Declarator declarator;
  1650. X
  1651. X    p = (Parameter *)malloc((unsigned)sizeof(Parameter));
  1652. X    new_declarator(&declarator, name, name);
  1653. X    new_parameter(p, NULL, &declarator);
  1654. X
  1655. X    to->first = from->first;
  1656. X    if (to->first == NULL) {
  1657. X    to->first = p;
  1658. X    } else {
  1659. X    from->last->next = p;
  1660. X    }
  1661. X    to->last = p;
  1662. X    p->next = NULL;
  1663. X}
  1664. X
  1665. X/* Initialize a declarator.
  1666. X */
  1667. Xvoid
  1668. Xnew_declarator (d, name, text)
  1669. XDeclarator *d;
  1670. Xchar *name, *text;
  1671. X{
  1672. X    d->name = strdup(name);
  1673. X    d->text = strdup(text);
  1674. X    d->func_def = FUNC_NONE;
  1675. X    d->params.first = d->params.last = NULL;
  1676. X}
  1677. X
  1678. X/* Free storage used by a declarator.
  1679. X */
  1680. Xvoid
  1681. Xfree_declarator (d)
  1682. XDeclarator *d;
  1683. X{
  1684. X    free(d->name);
  1685. X    free(d->text);
  1686. X    free_param_list(&(d->params));
  1687. X}
  1688. X
  1689. X/* Initialize a declarator list and add the given declarator to it.
  1690. X */
  1691. Xvoid
  1692. Xnew_decl_list (decl_list, declarator)
  1693. XDeclaratorList *decl_list;
  1694. XDeclarator *declarator;
  1695. X{
  1696. X    Declarator *d;
  1697. X
  1698. X    d = (Declarator *)malloc((unsigned)sizeof(Declarator));
  1699. X    *d = *declarator;
  1700. X
  1701. X    decl_list->first = decl_list->last = d;
  1702. X    d->next = NULL;
  1703. X}
  1704. X
  1705. X/* Add the declarator to the declarator list.
  1706. X */
  1707. Xvoid
  1708. Xadd_decl_list (to, from, declarator)
  1709. XDeclaratorList *to, *from;
  1710. XDeclarator *declarator;
  1711. X{
  1712. X    Declarator *d;
  1713. X
  1714. X    d = (Declarator *)malloc((unsigned)sizeof(Declarator));
  1715. X    *d = *declarator;
  1716. X
  1717. X    to->first = from->first;
  1718. X    from->last->next = d;
  1719. X    to->last = d;
  1720. X    to->last->next = NULL;
  1721. X}
  1722. X
  1723. X/* Free storage used by the declarators in the declarator list.
  1724. X */
  1725. Xvoid
  1726. Xfree_decl_list (decl_list)
  1727. XDeclaratorList *decl_list;
  1728. X{
  1729. X    Declarator *d, *next;
  1730. X
  1731. X    d = decl_list->first;
  1732. X    while (d != NULL) {
  1733. X    next = d->next;
  1734. X    free_declarator(d);
  1735. X    free((char *)d);
  1736. X    d = next;
  1737. X    }
  1738. X}
  1739. X
  1740. X/* Search the list of parameters for a matching parameter name.
  1741. X * Return a pointer to the matching parameter or NULL if not found.
  1742. X */
  1743. Xstatic Parameter *
  1744. Xsearch_parameter_list (params, name)
  1745. XParameterList *params;
  1746. Xchar *name;
  1747. X{
  1748. X    Parameter *p;
  1749. X
  1750. X    for (p = params->first; p != NULL; p = p->next) {
  1751. X    if (strcmp(p->declarator.name, name) == 0)
  1752. X        return p;
  1753. X    }
  1754. X    return (Parameter *)NULL;
  1755. X}
  1756. X
  1757. X/* This routine is called to generate function prototypes from traditional
  1758. X * style function definitions.  For each parameter name in the declarator
  1759. X * list, find the matching parameter name in the parameter list and set
  1760. X * that parameter's declaration specifier.
  1761. X * This is also where we promote formal parameters.  Parameters of type
  1762. X * "char", "unsigned char", "short", or "unsigned short" get promoted to
  1763. X * "int".  Parameters of type "float" are promoted to "double".
  1764. X */
  1765. Xvoid
  1766. Xset_param_types (params, decl_spec, declarators)
  1767. XParameterList *params;
  1768. XDeclSpec *decl_spec;
  1769. XDeclaratorList *declarators;
  1770. X{
  1771. X    Declarator *d;
  1772. X    Parameter *p;
  1773. X    char *decl_spec_text, *s;
  1774. X
  1775. X    for (d = declarators->first; d != NULL; d = d->next) {
  1776. X    /* Search the parameter list for a matching name. */
  1777. X    p = search_parameter_list(params, d->name);
  1778. X    if (p == NULL) {
  1779. X        output_error();
  1780. X        fprintf(stderr, "declared argument \"%s\" is missing\n", d->name);
  1781. X    } else {
  1782. X        p->declarator.text = strdup(d->text);
  1783. X        decl_spec_text = decl_spec->text;
  1784. X        if (promote_param && strcmp(d->text, d->name) == 0) {
  1785. X            s = rindex(decl_spec_text, ' ');
  1786. X        s = (s != NULL) ? s+1 : decl_spec_text;
  1787. X        if (strcmp(s, "char") == 0 || strcmp(s, "short") == 0)
  1788. X            decl_spec_text = "int";
  1789. X        else if (strcmp(s, "float") == 0)
  1790. X            decl_spec_text = "double";
  1791. X        }
  1792. X        p->decl_spec.text = strdup(decl_spec_text);
  1793. X    }
  1794. X    }
  1795. X}
  1796. X
  1797. X/* Output a declaration specifier for an external declaration.
  1798. X */
  1799. Xstatic void
  1800. Xoutput_decl_spec (decl_spec)
  1801. XDeclSpec *decl_spec;
  1802. X{
  1803. X    if (extern_out && (decl_spec->flags & DE_STATIC) == 0) {
  1804. X    if (strstr(decl_spec->text, "extern") == NULL) {
  1805. X        put_string("extern ");
  1806. X    }
  1807. X    }
  1808. X    put_string(decl_spec->text);
  1809. X}
  1810. X
  1811. Xstatic void output_parameters();
  1812. X
  1813. X/* Output a declarator.
  1814. X */
  1815. Xstatic void
  1816. Xoutput_declarator (d)
  1817. XDeclarator *d;
  1818. X{
  1819. X    char *s;
  1820. X
  1821. X    if (d->func_def == FUNC_NONE) {
  1822. X    put_string(d->text);
  1823. X    } else {
  1824. X    if ((s = strstr(d->text, "()")) != NULL) {
  1825. X        *s = '\0';
  1826. X        put_string(d->text);
  1827. X        put_string(declarator_suffix);
  1828. X        if (proto_style == PROTO_MACRO)
  1829. X        printf(" %s(", macro_name);
  1830. X        fputc(*s++ = '(', stdout);
  1831. X        output_parameters(&(d->params));
  1832. X        fputc(*s++, stdout);
  1833. X        if (proto_style == PROTO_MACRO)
  1834. X        putchar(')');
  1835. X        put_string(s);
  1836. X    }
  1837. X    }
  1838. X}
  1839. X
  1840. X/* Output a function parameter.
  1841. X */
  1842. Xstatic void
  1843. Xoutput_parameter (p)
  1844. XParameter *p;
  1845. X{
  1846. X    char *s;
  1847. X
  1848. X    put_string(p->decl_spec.text);
  1849. X    if (proto_style == PROTO_ABSTRACT && strlen(p->declarator.name) > 0) {
  1850. X    s = strstr(p->declarator.text, p->declarator.name);
  1851. X    *s = '\0';
  1852. X    printf(" %s/*%s*/%s", p->declarator.text, p->declarator.name,
  1853. X        s + strlen(p->declarator.name));
  1854. X    *s = *(p->declarator.name);
  1855. X    } else {
  1856. X    if (strlen(p->declarator.text) > 0) {
  1857. X        putchar(' ');
  1858. X        output_declarator(&(p->declarator));
  1859. X    }
  1860. X    }
  1861. X}
  1862. X
  1863. X/* Output the list of function parameters.
  1864. X */
  1865. Xstatic void
  1866. Xoutput_parameters (params)
  1867. XParameterList *params;
  1868. X{
  1869. X    Parameter *p;
  1870. X
  1871. X    if (proto_style == PROTO_TRADITIONAL)
  1872. X    put_string("/*");
  1873. X
  1874. X    p = params->first;
  1875. X    if (p == NULL ||
  1876. X        (strcmp(p->decl_spec.text, "void") == 0 &&
  1877. X     strlen(p->declarator.text) == 0)) {
  1878. X    put_string("void");
  1879. X    } else {
  1880. X    put_string(first_param_prefix);
  1881. X    output_parameter(p);
  1882. X    p = p->next;
  1883. X    while (p != NULL) {
  1884. X        putchar(',');
  1885. X        put_string(middle_param_prefix);
  1886. X        output_parameter(p);
  1887. X        p = p->next;
  1888. X    }
  1889. X    put_string(last_param_suffix);
  1890. X    }
  1891. X
  1892. X    if (proto_style == PROTO_TRADITIONAL)
  1893. X    put_string("*/");
  1894. X}
  1895. X
  1896. X/* Output variable declarations.
  1897. X */
  1898. Xvoid
  1899. Xoutput_declarations (decl_spec, decl_list)
  1900. XDeclSpec *decl_spec;        /* declaration specifier */
  1901. XDeclaratorList *decl_list;    /* list of declared variables */
  1902. X{
  1903. X    Declarator *d;
  1904. X
  1905. X    if (!variables_out || (decl_spec->flags & DE_JUNK))
  1906. X    return;
  1907. X    if (!static_out && (decl_spec->flags & DE_STATIC))
  1908. X    return;
  1909. X
  1910. X    for (d = decl_list->first; d != NULL; d = d->next) {
  1911. X    if (d->func_def == FUNC_NONE) {
  1912. X        put_string(decl_spec_prefix);
  1913. X        output_decl_spec(decl_spec);
  1914. X        putchar(' ');
  1915. X        output_declarator(d);
  1916. X        put_string(";\n");
  1917. X    }
  1918. X    }
  1919. X}
  1920. X
  1921. X/* Output a function prototype.
  1922. X */
  1923. Xvoid
  1924. Xoutput_prototype (decl_spec, declarator)
  1925. XDeclSpec *decl_spec;
  1926. XDeclarator *declarator;
  1927. X{
  1928. X    Parameter *p;
  1929. X
  1930. X    if (proto_style == PROTO_NONE)
  1931. X    return;
  1932. X    if (decl_spec->flags & DE_JUNK)
  1933. X    return;
  1934. X    if (!static_out && (decl_spec->flags & DE_STATIC))
  1935. X    return;
  1936. X
  1937. X    /* Check for parameter names with no declaration specifiers.  This
  1938. X     * happens when a parameter name appears in the identifier list of a
  1939. X     * function definition but does not appear in the parameter declaration
  1940. X     * part.  The default type in this cause is "int".
  1941. X     */
  1942. X    for (p = declarator->params.first; p != NULL; p = p->next) {
  1943. X    if (strlen(p->decl_spec.text) == 0) {
  1944. X        free(p->decl_spec.text);
  1945. X        p->decl_spec.text = strdup("int");
  1946. X    }
  1947. X    }
  1948. X
  1949. X    put_string(decl_spec_prefix);
  1950. X    output_decl_spec(decl_spec);
  1951. X    put_string(declarator_prefix);
  1952. X    output_declarator(declarator);
  1953. X    put_string(";\n");
  1954. X}
  1955. END_OF_semantic.c
  1956. if test 10639 -ne `wc -c <semantic.c`; then
  1957.     echo shar: \"semantic.c\" unpacked with wrong size!
  1958. fi
  1959. # end of overwriting check
  1960. fi
  1961. if test -f string.c -a "${1}" != "-c" ; then 
  1962.   echo shar: Will not over-write existing file \"string.c\"
  1963. else
  1964. echo shar: Extracting \"string.c\" \(795 characters\)
  1965. sed "s/^X//" >string.c <<'END_OF_string.c'
  1966. X/* $Id: string.c 2.1 91/02/28 11:16:33 cthuang Exp $
  1967. X *
  1968. X * Some string handling routines
  1969. X */
  1970. X#include <stdio.h>
  1971. X#include "config.h"
  1972. X
  1973. X/* Copy the string into an allocated memory block.
  1974. X * Return a pointer to the copy.
  1975. X */
  1976. Xchar *
  1977. Xstrdup (s)
  1978. Xchar *s;
  1979. X{
  1980. X    char *dest;
  1981. X
  1982. X    if ((dest = malloc((unsigned)(strlen(s)+1))) == NULL) {
  1983. X    fprintf(stderr, "No memory to duplicate string.\n");
  1984. X    exit(1);
  1985. X    }
  1986. X    strcpy(dest, s);
  1987. X    return dest;
  1988. X}
  1989. X
  1990. X/* Return a pointer to the first occurence of the substring 
  1991. X * within the string, or NULL if not found.
  1992. X */
  1993. Xchar *
  1994. Xstrstr (src, key)
  1995. Xchar *src, *key;
  1996. X{
  1997. X    char *s;
  1998. X    int keylen;
  1999. X
  2000. X    keylen = strlen(key);
  2001. X    s = index(src, *key);
  2002. X    while (s != NULL) {
  2003. X    if (strncmp(s, key, keylen) == 0)
  2004. X        return s;
  2005. X    s = index(s+1, *key);
  2006. X    }
  2007. X    return NULL;
  2008. X}
  2009. END_OF_string.c
  2010. if test 795 -ne `wc -c <string.c`; then
  2011.     echo shar: \"string.c\" unpacked with wrong size!
  2012. fi
  2013. # end of overwriting check
  2014. fi
  2015. if test -f symbol.c -a "${1}" != "-c" ; then 
  2016.   echo shar: Will not over-write existing file \"symbol.c\"
  2017. else
  2018. echo shar: Extracting \"symbol.c\" \(1900 characters\)
  2019. sed "s/^X//" >symbol.c <<'END_OF_symbol.c'
  2020. X/* $Id: symbol.c 2.1 91/02/28 11:16:35 cthuang Exp $
  2021. X *
  2022. X * Symbol table maintenance. Implements an abstract data type called
  2023. X * the symbol table.
  2024. X */
  2025. X#include <stdio.h>
  2026. X#include "config.h"
  2027. X#include "symbol.h"
  2028. X
  2029. X/* Create a symbol table.
  2030. X * Return a pointer to the symbol table or NULL if an error occurs.
  2031. X */
  2032. XSymbolTable *
  2033. Xcreate_symbol_table ()
  2034. X{
  2035. X    SymbolTable *symtab;
  2036. X    int i;
  2037. X
  2038. X    if ((symtab = (SymbolTable *)malloc(sizeof(SymbolTable))) != NULL) {
  2039. X    for (i = 0; i < SYM_MAX_HASH; ++i)
  2040. X        symtab->bucket[i] = NULL;
  2041. X    }
  2042. X    return symtab;
  2043. X}
  2044. X
  2045. X
  2046. X/* This is a simple hash function mapping a symbol name to a hash bucket. */
  2047. X
  2048. Xstatic int
  2049. Xhash (name)
  2050. Xchar *name;
  2051. X{
  2052. X    return (name[0] + name[1] + strlen(name)) % SYM_MAX_HASH;
  2053. X}
  2054. X
  2055. X
  2056. X/* Search the list of symbols <list> for the symbol <name>.
  2057. X * Return a pointer to the symbol or NULL if not found.
  2058. X */
  2059. Xstatic Symbol *
  2060. Xsearch_symbol_list (list, name)
  2061. XSymbol *list;
  2062. Xchar *name;
  2063. X{
  2064. X    Symbol *sym;
  2065. X
  2066. X    for (sym = list; sym != NULL; sym = sym->next) {
  2067. X    if (strcmp(sym->name, name) == 0)
  2068. X        return sym;
  2069. X    }
  2070. X    return NULL;
  2071. X}
  2072. X
  2073. X
  2074. X/* Look for symbol <name> in symbol table <symtab>.
  2075. X * Return a pointer to the symbol or NULL if not found.
  2076. X */
  2077. XSymbol *
  2078. Xfind_symbol (symtab, name)
  2079. XSymbolTable *symtab;
  2080. Xchar *name;
  2081. X{
  2082. X    return search_symbol_list(symtab->bucket[hash(name)], name);
  2083. X}
  2084. X
  2085. X
  2086. X/* If the symbol <name> does not already exist in symbol table <symtab>,
  2087. X * then add the symbol to the symbol table.
  2088. X * Return a pointer to the symbol or NULL on an error.
  2089. X */
  2090. XSymbol *
  2091. Xnew_symbol (symtab, name)
  2092. XSymbolTable *symtab;    /* symbol table */
  2093. Xchar *name;        /* symbol name */
  2094. X{
  2095. X    Symbol *sym;
  2096. X    int i;
  2097. X
  2098. X    if ((sym = find_symbol(symtab, name)) == NULL) {
  2099. X    if ((sym = (Symbol *)malloc(sizeof(Symbol))) != NULL) {
  2100. X        sym->name = strdup(name);
  2101. X        i = hash(name);
  2102. X        sym->next = symtab->bucket[i];
  2103. X        symtab->bucket[i] = sym;
  2104. X    }
  2105. X    }
  2106. X    return sym;
  2107. X}
  2108. END_OF_symbol.c
  2109. if test 1900 -ne `wc -c <symbol.c`; then
  2110.     echo shar: \"symbol.c\" unpacked with wrong size!
  2111. fi
  2112. # end of overwriting check
  2113. fi
  2114. echo shar: End of shell archive.
  2115. exit 0
  2116. -- 
  2117. Chin Huang  cthuang@contact.uucp  chin.huang@canrem.uucp
  2118.  
  2119. exit 0 # Just in case...
  2120. -- 
  2121. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  2122. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  2123. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  2124. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  2125.